package com.bootdo.cms.controller;

import com.bootdo.cms.domain.ChannelDO;
import com.bootdo.cms.domain.VideoDO;
import com.bootdo.cms.service.ChannelService;
import com.bootdo.cms.service.CmsFileService;
import com.bootdo.cms.service.MemberService;
import com.bootdo.cms.service.VideoService;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.ShiroUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @author 空白
 * @version 1.0
 * Create by 2019/11/17 00:58
 */
@Controller
@RequestMapping("/front")
public class FrontController {
    String prefix = "front";

    @Autowired
    private ChannelService channelService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private BootdoConfig bootdoConfig;

    @Autowired
    private CmsFileService cmsFileService;

    @Autowired
    private MemberService memberService;
    /**
     * 第一次进入首页，显示所有视频
     * 
     * @param model
     * @return
     */
    @GetMapping("/index")
    public String index(Map<String, Object> map, Model model) {
        List<Tree<ChannelDO>> channels = channelService.listChannelTree();
        // List<VideoDO> videoDOs = videoService.list(map);
        model.addAttribute("channels", channels);
        // model.addAttribute("videos", videoDOs);
        return prefix + "/index";
    }

    /**
     * 根据栏目id查询栏目下所有视频
     *
     * @param map
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/index/{id}")
    public String listByChannelId(Map<String, Object> map, Model model, @PathVariable Long id) {
        // 查询所有栏目
        List<Tree<ChannelDO>> channels = channelService.listChannelTree();
        // 查询此节点下所有节点
        // List<Long> childIds = channelService.listChildrenIds(id);
        // childIds.add(id);
        map.put("channelId", id);
        // map.put("channelIds",childIds);
        List<VideoDO> videoDOs = videoService.list(map);
        model.addAttribute("channels", channels);
        model.addAttribute("videos", videoDOs);
        return prefix + "/video";
    }

    /**
     * 根据视频名称查询对应视频
     *
     * @param map
     * @param model
     * @param videoName
     * @return
     */
    @GetMapping("/search/{qt}")
    public String listByName(Map<String, Object> map, Model model, @PathVariable(value = "qt") String videoName) {
        List<Tree<ChannelDO>> channels = channelService.listChannelTree();
        map.put("name", videoName);
        // map.put("channelIds",childIds);
        List<VideoDO> videoDOs = videoService.list(map);
        model.addAttribute("channels", channels);
        model.addAttribute("videos", videoDOs);
        return prefix + "/video";
    }

    // /**
    //  * 跳转至播放页
    //  *
    //  * @param model
    //  * @param id
    //  * @return
    //  */
//    @GetMapping("/show/{id}")
//    public String show(Model model, @PathVariable Long id) {
//        List<Tree<ChannelDO>> channels = channelService.listChannelTree();
//        VideoDO video = videoService.get(id);
//        String url = video.getUrl();
//        String path = Constant.PATH;
//        StringBuffer realUrl = new StringBuffer(path);
//        realUrl.append("\\" + url + ".mp4");
//        String videoUrl = realUrl.toString().replace("\\","/");
//        model.addAttribute("url",url);
//        model.addAttribute("channels", channels);
//        model.addAttribute("video", video);
//        return prefix + "/videoShow";
//    }

    @GetMapping("/getVideo/{url}")
    public void getVideo(HttpServletRequest request, HttpServletResponse response, @PathVariable String url) {
        String path = bootdoConfig.getUploadPath() +
                "video" + File.separator  +
                url + ".mp4";
        path.replace("\\","/");
        System.out.println(path);
        File file = new File(path);
        try {
            InputStream inputStream = new FileInputStream(file);
            response.setHeader("Content-type","video/mp4;charset=UTF-8");
            IOUtils.copy(inputStream,response.getOutputStream());
            response.flushBuffer();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转到个人中心
     * @param model
     * @return
     */
    @GetMapping("/message")
    public String message(Model model) {
        List<Tree<ChannelDO>> channels = channelService.listChannelTree();
        model.addAttribute("channels", channels);
        model.addAttribute("member", memberService.get(ShiroUtils.getMember().getMemberId()));
        return prefix + "/checkout";
    }

    /**
     * 跳转投稿中心
     * @param model
     * @return
     */
    @GetMapping("/upload")
    public String upload(Model model){
        List<Tree<ChannelDO>> channels = channelService.listChannelTree();
        model.addAttribute("channels", channels);
        return prefix + "/upload"   ;
    }

    @GetMapping("/info")
    public String info(Model model){
        List<Tree<ChannelDO>> channels = channelService.listChannelTree();
        model.addAttribute("channels", channels);
    	model.addAttribute("list",cmsFileService.getAll());
        return prefix + "/information";
    }

    @GetMapping("/info/{channelId}")
    public String getInfoByType(@PathVariable(value = "channelId") String channelId, Model model) {
        List<Tree<ChannelDO>> channels = channelService.listChannelTree();
        model.addAttribute("channels", channels);
    	model.addAttribute("list",cmsFileService.selectByChannelId(channelId));
		return prefix + "/information";
	}

	@PostMapping("/search")
	public String searchFile(@RequestParam String fileName, Model model) {
        List<Tree<ChannelDO>> channels = channelService.listChannelTree();
        model.addAttribute("channels", channels);
        model.addAttribute("list",cmsFileService.selectBySearch(fileName));
        return prefix + "/information";
    }
}
